package cn.edu.sgu.www.pms.controller;

import cn.edu.sgu.www.pms.annotation.FeignService;
import cn.edu.sgu.www.pms.consts.CustomConstants;
import cn.edu.sgu.www.pms.consts.CustomMessages;
import cn.edu.sgu.www.pms.consts.MimeTypes;
import cn.edu.sgu.www.pms.dto.EmailDTO;
import cn.edu.sgu.www.pms.entity.Email;
import cn.edu.sgu.www.pms.restful.JsonResult;
import cn.edu.sgu.www.pms.service.EmailService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "邮箱管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/email", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class EmailController {

    private final EmailService emailService;

    @Autowired
    public EmailController(EmailService emailService) {
        this.emailService = emailService;
    }

    @FeignService
    @ApiOperation("启用用户邮箱")
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public JsonResult<Void> enable(
            @ApiParam(name = "id", value = "邮箱ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        emailService.enable(id);

        return JsonResult.success("启用成功");
    }

    @FeignService
    @ApiOperation("添加用户邮箱")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@RequestBody @Validated EmailDTO emailDTO) {
        emailService.insert(emailDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @FeignService
    @ApiOperation("通过ID删除用户邮箱")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public JsonResult<Void> deleteById(
            @ApiParam(name = "id", value = "邮箱ID", required = true)
            @NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
        emailService.deleteById(id);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @FeignService
    @ApiOperation("通过ID修改用户邮箱")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@RequestBody @Validated EmailDTO emailDTO) {
        emailService.updateById(emailDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @FeignService
    @ApiOperation("通过用户ID查询邮箱列表")
    @RequestMapping(value = "/selectByUserId", method = RequestMethod.GET)
    public JsonResult<List<Email>> selectByUserId(
            @ApiParam(name = "userId", value = "用户ID", required = true)
            @NotBlank(message = "用户" + CustomMessages.ID_NOT_NULL) String userId) {
        List<Email> list = emailService.selectByUserId(userId);

        return JsonResult.success(null, list);
    }

}